var displayWidth = 640;
var displayHeight = 480;
		
var graphics;
var input;
		
var polygonPoints = new Array();
var pathfinder = new Pathfinder();
var grid = null;
var playerPoint = null;
var playerPath = null;

pathfinder.checkNode = function(column, row)
{
	var node = grid.nodes[(row * grid.gridWidth) + column];
	return node != null && node.isValid;
}
		
$(document).ready(function ()
{
	graphics = new Graphics(displayWidth, displayHeight);
	graphics.clearColor = '#559cd0';
	
	input = new Input();
	input.onMouseDown = function(which, cursorX, cursorY)
	{
		
	};
	
	input.onMouseUp = function(which, cursorX, cursorY)
	{
		var destination = findGridNode(cursorX, cursorY);
		if(playerPoint == null)
		{
			if(destination != null)
				playerPoint = new Point(destination.column, destination.row);
		}
		else
		{
			var path = pathfinder.findPath(playerPoint, new Point(destination.column, destination.row));
			if(path == null)
			{
				playerPath = null;
				return;
			}
			
			var waypoints = new Array();
			for(var index = 0; index < path.points.length; ++index)
			{
				var node = grid.nodes[(path.points[index].y * grid.gridWidth) + path.points[index].x];
				var nodeCenter = node.bounds.getCenter();
				waypoints.push(nodeCenter);
			}
			
			playerPath = new Path();
			playerPath.addWaypoints(waypoints);
		}
	};
	
	input.onMouseMove = function(mouseX, mouseY)
	{
	};
	
		
	polygonPoints.push(new Point(116, 180));
	polygonPoints.push(new Point(355, 63));
	polygonPoints.push(new Point(487, 119));
	polygonPoints.push(new Point(538, 304));
	polygonPoints.push(new Point(275, 397));
	polygonPoints.push(new Point(134, 375));
	polygonPoints.push(new Point(262, 328));
	polygonPoints.push(new Point(392, 272));
	polygonPoints.push(new Point(315, 178));
	polygonPoints.push(new Point(116, 180));
	
	grid = createGrid(polygonPoints, 16, 16);
	
	startGameLoop(update, draw, 30);
});

function findGridNode(x, y)
{
	for(var row = 0; row < grid.gridHeight; ++row)
	{
		for(var column = 0; column < grid.gridWidth; ++column)
		{
			var node = grid.nodes[(row * grid.gridWidth) + column];
			if(x > node.bounds.x && x < node.bounds.x + node.bounds.width &&
				y > node.bounds.y && y < node.bounds.y + node.bounds.height)
				return { "column": column, "row": row, "node": node };
		}
	}
	return null;
}

function update(delta)
{
	
}

function draw()
{
	graphics.clear();
	
	for(var row = 0; row < grid.gridHeight; ++row)
	{
		for(var column = 0; column < grid.gridWidth; ++column)
		{
			var node = grid.nodes[(row * grid.gridWidth) + column];
			graphics.drawRectangle(node.bounds.x, node.bounds.y, node.bounds.width, node.bounds.height, node.isValid ? "#ff0000" : "#000000", 1);
		}
	}
		
	for(var index = 1; index < polygonPoints.length; ++index)
	{
		var point1 = polygonPoints[index - 1];
		var point2 = polygonPoints[index];
		
		graphics.drawLine(point1.x, point1.y, point2.x, point2.y, "#ffff00", 1);
	}
	
	if(playerPoint != null)
	{
		var node = grid.nodes[(playerPoint.y * grid.gridWidth) + playerPoint.x];
		graphics.fillRectangle(node.bounds.x, node.bounds.y, node.bounds.width, node.bounds.height, "#ff0000");
	}
	
	if(playerPath != null)
	{
		for(var index = 1 ; index < playerPath.waypoints.length; ++index)
		{
			var startPoint = playerPath.waypoints[index - 1];
			var endPoint = playerPath.waypoints[index];
			
			var startPointArea = new Rectangle(startPoint.x - 3, startPoint.y - 3, 6, 6);
			var endPointArea = new Rectangle(endPoint.x - 3, endPoint.y - 3, 6, 6);
			
			if(index == 1)
				graphics.fillRectangle(startPointArea.x, startPointArea.y, startPointArea.width, startPointArea.height, "#ffff00");
			
			graphics.fillRectangle(endPointArea.x, endPointArea.y, endPointArea.width, endPointArea.height, "#ffff00");
			graphics.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, "#00ff00", 1);
		}
	}
}